//
//  JBWHomeOriginalView.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/4.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit
import SnapKit
class JBWHomeOriginalView: UIView {

    //记录原微博的底部约束
    var originalViewBottomConstraint: Constraint?
    //提供一个模型属性 供外界赋值
    var statusViewModel: JBWStatusViewModel?{
        didSet{
            
        //赋值
            headImageView.jbw_setImage(withUrlString: statusViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            nameLabel.text = statusViewModel?.homeModel?.user?.name
            verifiedImageView.image = statusViewModel?.verifiedImage
            contentLabel.text = statusViewModel?.homeModel?.text
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            //卸载约束
            originalViewBottomConstraint?.deactivate()
            
            //判断是否有配配图
            if let pictures = statusViewModel?.homeModel?.pic_urls,pictures.count > 0 {
                
                //赋值
                pictureView.pictureList = pictures
                
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                //显示配图
                pictureView.isHidden = false
            } else {
                //没有配图
                snp.makeConstraints({ (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.isHidden = true
            }
            
            //时间
            timeLabel.text = statusViewModel?.createdAtText
            //微博来源
            sourceLabel.text = statusViewModel?.sourceText
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    




// 设置视图
private func setupUI(){
    backgroundColor = UIColor.white
    // 添加控件
    addSubview(headImageView)
    addSubview(nameLabel)
    addSubview(mbrankImageView)
    addSubview(timeLabel)
    addSubview(sourceLabel)
    addSubview(verifiedImageView)
    addSubview(contentLabel)
    addSubview(pictureView)
    // 添加约束
    headImageView.snp.makeConstraints { (make) in
        make.left.top.equalTo(10)
        make.size.equalTo(CGSize(width: 35, height: 35))
    }
    
    nameLabel.snp.makeConstraints { (make) in
        make.top.equalTo(headImageView)
        make.left.equalTo(headImageView.snp.right).offset(10)
    }
    
    mbrankImageView.snp.makeConstraints { (make) in
        make.left.equalTo(nameLabel.snp.right).offset(10)
        make.centerY.equalTo(nameLabel)
    }
    
    timeLabel.snp.makeConstraints { (make) in
        make.left.equalTo(nameLabel)
        make.bottom.equalTo(headImageView)
    }
    
    sourceLabel.snp.makeConstraints { (make) in
        make.bottom.equalTo(timeLabel)
        make.left.equalTo(timeLabel.snp.right).offset(10)
    }
    
    verifiedImageView.snp.makeConstraints { (make) in
        make.centerX.equalTo(headImageView.snp.right)
        make.centerY.equalTo(headImageView.snp.bottom)
    }
    
    contentLabel.snp.makeConstraints { (make) in
        make.left.equalTo(headImageView)
        make.top.equalTo(headImageView.snp.bottom).offset(10)
    }
    
    pictureView.snp.makeConstraints { (make) in
        make.left.equalTo(contentLabel)
        make.top.equalTo(contentLabel.snp.bottom).offset(10)
    }
    
    snp.makeConstraints { (make) in
         originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
    }
    
    
}


    // 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(named: "avatar_default_big")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(text: "昵称", textColor: UIColor.black, fontSize: JBWNormalFontSize)
    // 会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(named: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(text: "刚刚", textColor: JBWThemeColor, fontSize: JBWSmallFontSize)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(text: "来自 Smartisan", textColor: JBWThemeColor, fontSize: JBWSmallFontSize)
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(named: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: JBWNormalFontSize, maxWeight: JBWSCREENW - 20)
    //配图
    private lazy var pictureView: JBWHomePictureView = {
       let view = JBWHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
}
